<?php
/**
 * Created by PhpStorm.
 */

use Biz\service\activity\OutCoupon as sOutCoupon;
use Doraemon\model\Phone;
use Doraemon\service\Member\PhoneCrypto;


class ReceiveAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $data = ['is_new' => 1];
        $idMd5 = $this->getRequest()->getPost('id', '');
        $mobile = $this->getRequest()->getPost('mobile', '');

        $mobile = PhoneCrypto::encrypt($mobile, PhoneCrypto::CLIENT_BIZ, PhoneCrypto::VERSIN);
        $user = Phone::getInfoByPhone($mobile);

        if (!empty($user)) {
            $data = ['is_new' => 0];
        }

        try {

            $result = sOutCoupon::receive($idMd5, $mobile);

            if (isset($result['tip_msg'])) {
                throw new \Exception($result['tip_msg']);
            }
            $this->setSuccess($data);

        } catch (\Exception $e) {
            $this->setSuccess($data, 0, $e->getMessage());

        }
    }

}
